package com.foreknow.controller;

import com.foreknow.bean.Guest;
import com.foreknow.service.GuestService;
import com.foreknow.service.impl.GuestServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型: text/html
        resp.setContentType("text/html;charset=UTF-8");
        //客户端向服务器端请求的编码格式（否则会产生乱码） 默认：IOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        GuestService service = new GuestServiceImpl();
        List<Object> list = service.getQueryAll();
        //通过request.setAttribute("list",list);在服务器端保存list集合
        req.setAttribute("list",list);
        //转发资源到list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
//        out.println("<table>");
//        out.println("<tr>");
//        out.println("<td>id</td>");
//        out.println("<td>name</td>");
//        out.println("<td>phone</td>");
//        out.println("<td>email</td>");
//        out.println("<td>title</td>");
//        out.println("<td>content</td>");
//        out.println("<td>createdtime</td>");
//        out.println("</tr>");
//        for(int i = 0;i<list.size();i++){
//            Guest guest = (Guest)list.get(i);
//            out.println("<tr>");
//            out.println("<td>"+guest.getId()+"</td>");
//            out.println("<td>"+guest.getName()+"</td>");
//            out.println("<td>"+guest.getPhone()+"</td>");
//            out.println("<td>"+guest.getEmail()+"</td>");
//            out.println("<td>"+guest.getTitle()+"</td>");
//            out.println("<td>"+guest.getContent()+"</td>");
//            out.println("<td>"+guest.getCreatedTime()+"</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
